﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.BLL;
using CMN.Common;
using CMN.Entity;
using CMN.Models.Interfaces;
namespace CMN.Models
{
    /// <summary>
    /// 角色应用权限表Service
    /// </summary>
    public class Sys_RolePermissionService: IDataService<Sys_RolePermissionEntity>
    {
        /// <summary>
        /// 新增角色应用权限表
        /// </summary>
        /// <param name="Sys_RolePermissionEntity"></param>
        /// <returns>bool</returns>
        public string Insert(Sys_RolePermissionEntity sys_rolepermissionEntity)
        {
            sys_rolepermissionEntity.DataTable_Action = DataTableAction.Insert;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_RolePermissionInsertUpdateDelete(sys_rolepermissionEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 返回角色应用权限表查询列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="count">查询总个数</param>
        /// <returns>List</returns>
        public List<Sys_RolePermissionEntity> List(QueryParam qp,out int count)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_RolePermissionList(qp, out count);
        }

        /// <summary>
        /// 更新角色应用权限表
        /// </summary>
        /// <param name="Sys_RolePermission"></param>
        /// <returns>bool</returns>
        public string Update(Sys_RolePermissionEntity sys_rolepermissionEntity)
        {
            BLLHelper bll = new BLLHelper();
            Sys_RolePermissionEntity temp = bll.Sys_RolePermissionByCodeAndRoleId(sys_rolepermissionEntity.P_PageCode, sys_rolepermissionEntity.P_RoleID);
            
            if (temp != null)
            {
                temp.DataTable_Action = DataTableAction.Update;
                temp.P_Value = sys_rolepermissionEntity.P_Value;
              
            }
            else
            {
                temp = sys_rolepermissionEntity;
                temp.DataTable_Action = DataTableAction.Insert;
                
            }
            if ((temp.P_Value != 0) || (temp.DataTable_Action != DataTableAction.Insert))
            {
              
                if (bll.Sys_RolePermissionInsertUpdateDelete(temp) > 0)
                {
                    return "";
                }
                else
                {
                    return "失败";
                }
            }
            else
            {
              
                return "失败";
            }
        }

        /// <summary>
        /// 删除角色应用权限表
        /// </summary>
        /// <param name="Sys_RolePermission"></param>
        /// <returns>bool</returns>
        public string Delete(Sys_RolePermissionEntity sys_rolepermissionEntity)
        {
            sys_rolepermissionEntity.DataTable_Action = DataTableAction.Delete;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_RolePermissionInsertUpdateDelete(sys_rolepermissionEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 批量删角色应用权限表
        /// </summary>
        /// <param name="idList">删除id列表  2_8_9_45</param>
        /// <returns>bool</returns>
        public string DeleteList(string idList)
        {
            string[] list =Utils.SplitString(idList,"_");
            string strDel=Utils.ArrayToString(list,",",false);
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_RolePermissionDelList(strDel) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }

        }

        /// <summary>
        /// 返回单个角色应用权限表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Sys_RolePermissionEntity</returns>
        public Sys_RolePermissionEntity Look(int id)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_RolePermissionDisp(id);
        }
    }
}
